﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract NetoBruto typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class NetoBrutoDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<NetoBrutoDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.NetoBruto _currentNetoBruto = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxNetoBrutoDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxNetoBrutoErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxNetoBrutoBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the StopaPrireza property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxStopaPrirezaDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Kpr property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxKprDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Kpr25 property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxKpr25DataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Kpr35 property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxKpr35DataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Kpr45 property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxKpr45DataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.NetoBruto> _NetoBrutoList;
				
		/// <summary> 
		/// The list of NetoBruto to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.NetoBruto> NetoBrutoList
		{
			get {return this._NetoBrutoList;}
			set
			{
				this._NetoBrutoList = value;
				this.uxNetoBrutoBindingSource.DataSource = null;
				this.uxNetoBrutoBindingSource.DataSource = value;
				this.uxNetoBrutoDataGridView.DataSource = null;
				this.uxNetoBrutoDataGridView.DataSource = this.uxNetoBrutoBindingSource;				
				//this.uxNetoBrutoBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxNetoBrutoBindingSource_ListChanged);
				this.uxNetoBrutoBindingSource.CurrentItemChanged += new System.EventHandler(OnNetoBrutoBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnNetoBrutoBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentNetoBruto = uxNetoBrutoBindingSource.Current as Entities.NetoBruto;
			
			if (_currentNetoBruto != null)
			{
				_currentNetoBruto.Validate();
			}
			//_NetoBruto.Validate();
			OnCurrentEntityChanged();
		}

		//void uxNetoBrutoBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.NetoBruto"/> instance.
		/// </summary>
		public Entities.NetoBruto SelectedNetoBruto
		{
			get {return this._currentNetoBruto;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxNetoBrutoDataGridView.VirtualMode;}
			set
			{
				this.uxNetoBrutoDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxNetoBrutoDataGridView.AllowUserToAddRows;}
			set {this.uxNetoBrutoDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxNetoBrutoDataGridView.AllowUserToDeleteRows;}
			set {this.uxNetoBrutoDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxNetoBrutoDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxNetoBrutoDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="NetoBrutoDataGridViewBase"/> class.
		/// </summary>
		public NetoBrutoDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxNetoBrutoDataGridView = new System.Windows.Forms.DataGridView();
			this.uxNetoBrutoBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxNetoBrutoErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxStopaPrirezaDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxKprDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxKpr25DataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxKpr35DataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxKpr45DataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxNetoBrutoDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNetoBrutoBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNetoBrutoErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxNetoBrutoErrorProvider
			// 
			this.uxNetoBrutoErrorProvider.ContainerControl = this;
			this.uxNetoBrutoErrorProvider.DataSource = this.uxNetoBrutoBindingSource;						
			// 
			// uxNetoBrutoDataGridView
			// 
			this.uxNetoBrutoDataGridView.AutoGenerateColumns = false;
			this.uxNetoBrutoDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxNetoBrutoDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxStopaPrirezaDataGridViewColumn,
		this.uxKprDataGridViewColumn,
		this.uxKpr25DataGridViewColumn,
		this.uxKpr35DataGridViewColumn,
		this.uxKpr45DataGridViewColumn			});
			this.uxNetoBrutoDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxNetoBrutoDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxNetoBrutoDataGridView.Name = "uxNetoBrutoDataGridView";
			this.uxNetoBrutoDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxNetoBrutoDataGridView.TabIndex = 0;	
			this.uxNetoBrutoDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxNetoBrutoDataGridView.EnableHeadersVisualStyles = false;
			this.uxNetoBrutoDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnNetoBrutoDataGridViewDataError);
			this.uxNetoBrutoDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNetoBrutoDataGridViewCellValueNeeded);
			this.uxNetoBrutoDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnNetoBrutoDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxStopaPrirezaDataGridViewColumn
			//
			this.uxStopaPrirezaDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxStopaPrirezaDataGridViewColumn.DataPropertyName = "StopaPrireza";
			this.uxStopaPrirezaDataGridViewColumn.HeaderText = "StopaPrireza";
			this.uxStopaPrirezaDataGridViewColumn.Name = "uxStopaPrirezaDataGridViewColumn";
			this.uxStopaPrirezaDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxStopaPrirezaDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxStopaPrirezaDataGridViewColumn.ReadOnly = false;		
			//
			// uxKprDataGridViewColumn
			//
			this.uxKprDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxKprDataGridViewColumn.DataPropertyName = "Kpr";
			this.uxKprDataGridViewColumn.HeaderText = "Kpr";
			this.uxKprDataGridViewColumn.Name = "uxKprDataGridViewColumn";
			this.uxKprDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxKprDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxKprDataGridViewColumn.ReadOnly = false;		
			//
			// uxKpr25DataGridViewColumn
			//
			this.uxKpr25DataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxKpr25DataGridViewColumn.DataPropertyName = "Kpr25";
			this.uxKpr25DataGridViewColumn.HeaderText = "Kpr25";
			this.uxKpr25DataGridViewColumn.Name = "uxKpr25DataGridViewColumn";
			this.uxKpr25DataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxKpr25DataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxKpr25DataGridViewColumn.ReadOnly = false;		
			//
			// uxKpr35DataGridViewColumn
			//
			this.uxKpr35DataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxKpr35DataGridViewColumn.DataPropertyName = "Kpr35";
			this.uxKpr35DataGridViewColumn.HeaderText = "Kpr35";
			this.uxKpr35DataGridViewColumn.Name = "uxKpr35DataGridViewColumn";
			this.uxKpr35DataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxKpr35DataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxKpr35DataGridViewColumn.ReadOnly = false;		
			//
			// uxKpr45DataGridViewColumn
			//
			this.uxKpr45DataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxKpr45DataGridViewColumn.DataPropertyName = "Kpr45";
			this.uxKpr45DataGridViewColumn.HeaderText = "Kpr45";
			this.uxKpr45DataGridViewColumn.Name = "uxKpr45DataGridViewColumn";
			this.uxKpr45DataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxKpr45DataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxKpr45DataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxNetoBrutoDataGridView);
			this.Name = "NetoBrutoDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxNetoBrutoErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNetoBrutoDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxNetoBrutoBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				NetoBrutoDataGridViewEventArgs args = new NetoBrutoDataGridViewEventArgs();
				args.NetoBruto = _currentNetoBruto;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class NetoBrutoDataGridViewEventArgs : System.EventArgs
		{
			private Entities.NetoBruto	_NetoBruto;
	
			/// <summary>
			/// the  Entities.NetoBruto instance.
			/// </summary>
			public Entities.NetoBruto NetoBruto
			{
				get { return _NetoBruto; }
				set { _NetoBruto = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxNetoBrutoDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnNetoBrutoDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxNetoBrutoDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNetoBrutoDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNetoBrutoDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = NetoBrutoList[e.RowIndex].ID;
						break;
					case "uxStopaPrirezaDataGridViewColumn":
						e.Value = NetoBrutoList[e.RowIndex].StopaPrireza;
						break;
					case "uxKprDataGridViewColumn":
						e.Value = NetoBrutoList[e.RowIndex].Kpr;
						break;
					case "uxKpr25DataGridViewColumn":
						e.Value = NetoBrutoList[e.RowIndex].Kpr25;
						break;
					case "uxKpr35DataGridViewColumn":
						e.Value = NetoBrutoList[e.RowIndex].Kpr35;
						break;
					case "uxKpr45DataGridViewColumn":
						e.Value = NetoBrutoList[e.RowIndex].Kpr45;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxNetoBrutoDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnNetoBrutoDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxNetoBrutoDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						NetoBrutoList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxStopaPrirezaDataGridViewColumn":
						NetoBrutoList[e.RowIndex].StopaPrireza = (System.Decimal?)e.Value;
						break;
					case "uxKprDataGridViewColumn":
						NetoBrutoList[e.RowIndex].Kpr = (System.Single?)e.Value;
						break;
					case "uxKpr25DataGridViewColumn":
						NetoBrutoList[e.RowIndex].Kpr25 = (System.Single?)e.Value;
						break;
					case "uxKpr35DataGridViewColumn":
						NetoBrutoList[e.RowIndex].Kpr35 = (System.Single?)e.Value;
						break;
					case "uxKpr45DataGridViewColumn":
						NetoBrutoList[e.RowIndex].Kpr45 = (System.Single?)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
